package org.grow.leave.controller;

import org.grow.leave.entity.EnumDto;
import org.grow.leave.entity.enums.ApplyState;
import org.grow.leave.entity.enums.AuditState;
import org.grow.leave.entity.enums.LeaveType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: xwg
 * @CreateDate: 2021/10/19
 */
@RestController
@RequestMapping("/leave/common")
public class CommonController {
    @GetMapping("/leaveTypes")
    public List<EnumDto> leaveTypes() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication);
        System.out.println(authentication.getPrincipal());
        System.out.println(authentication.getAuthorities());
        for (GrantedAuthority authority : authentication.getAuthorities()) {
            System.out.println(authority.getAuthority()+"a  "+authority.getClass());
        }
        return Arrays.stream(LeaveType.values())
                .map(x -> new EnumDto(
                        x.ordinal(),
                        x.name(),
                        x.getText()))
                .collect(Collectors.toList());
    }

    @GetMapping("/auditStates")
    public List<EnumDto> auditStates(){
        return Arrays.stream(AuditState.values())
                .map(x -> new EnumDto(
                        x.ordinal(),
                        x.name(),
                        x.getText()))
                .collect(Collectors.toList());
    }

    @GetMapping("/applyStates")
    public List<EnumDto> applyStates() {
        return Arrays.stream(ApplyState.values())
                .map(x -> new EnumDto(
                        x.ordinal(),
                        x.name(),
                        x.getText()))
                .collect(Collectors.toList());
    }

}
